package com.tanhua.manager.controller;

import com.tanhua.domain.db.Admin;
import com.tanhua.domain.vo.AdminVo;
import com.tanhua.manager.interceptors.AdminHolder;
import com.tanhua.manager.service.AdminService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/system")
public class SystemController {


    @Autowired
    private AdminService adminService;

//    登录时需要的验证码图片
    @GetMapping(value = "/users/verification",name ="登录时需要的验证码图片" )
    public ResponseEntity verification(HttpServletResponse response, String uuid) throws Exception{
        return adminService.verification(response,uuid);
    }
    @PostMapping(value = "/users/login",name ="登录" )
    public ResponseEntity login(@RequestBody Map map) throws Exception{
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        String verificationCode = (String) map.get("verificationCode");
        String uuid = (String) map.get("uuid");
        return adminService.login(username,password,verificationCode,uuid);
    }


    @PostMapping(value = "/users/profile",name ="获取登录人信息" )
    public ResponseEntity profile() throws Exception{
        Admin admin = AdminHolder.getAdmin();
        AdminVo adminVo = new AdminVo();
        adminVo.setId(admin.getId());
        adminVo.setUsername(admin.getUsername());
        adminVo.setAvatar(admin.getAvatar());
        return ResponseEntity.ok(adminVo);

    }
    @PostMapping(value = "/users/logout",name ="退出登录" )
    public ResponseEntity logout() throws Exception{
        return adminService.logout();

    }


}
